/***************************************************************************
 
    This file is part of Count_and_Check
    Copyright (C) 2019 uncle-lu

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/
#include "tools.h"

bool is_end(std::vector<char>::iterator t)
{
	if(*t == '/' && *(t-1) == '*')
		return true;
	return false;
}

bool is_begin(std::vector<char>::iterator t)
{
	if(*t == '/' && *(t+1) == '*')
		return true;
	return false;
}

int Count_Line(std::vector<char> *line)
{
	int count = 0;

	/*
	 * flag_1 : to sign if the code in the / * * / 
	 * flag_2 : to sign if the code in the //
	 * flag_3 : to sign whether there is code between the two newlines
	 */
	bool flag_1=false, flag_2=false, flag_3=false;
	if(*line->begin() == '/' && *(line->begin()+1) == '/')flag_2=true;

	for(std::vector<char>::iterator i = line->begin(); i != line->end(); i++)
	{
		if(flag_1 && is_end(i)) { flag_1 = false; continue; }
		if(flag_1)continue;
		if(is_begin(i)) { flag_1 = true; continue; }

		if(*i != '\n' )
			flag_3 = true;
		else
		{
			if( flag_3 && (!flag_2) )count++;
			flag_3 = false;
			flag_2 = false;
			if( i+1 == line->end() || i+2 ==line->end())continue;
			if( *(i+1) == '/' && *(i+2) == '/')flag_2 = true;
		}
	}

	if( (!flag_2) && ( !is_end(line->end()-1) ) && *(line->end()-1) != '\n' )count++;

	return count;
}
